package com.yjhb.android.sbz.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import com.borjabravo.readmoretextview.ReadMoreTextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.Comment;
import com.jojo.android.zxlib.view.CircleImageView;

import java.util.List;

/**
 * 创建者 zx
 * 创建时间 2020/12/1 17:37
 */
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentHolder> {
    private final String TAG = "CommentAdapter";

    private Context context;
    private List<Comment> comments;

    public CommentAdapter(Context context) {
        this.context = context;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    @NonNull
    @Override
    public CommentHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new CommentHolder(LayoutInflater.from(context).inflate(R.layout.item_comment, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CommentHolder commentHolder, int i) {
        try {
            Comment comment = comments.get(i);
            commentHolder.time_tv.setText(comment.getCreateTime());
            commentHolder.commentText_rtv.setText(comment.getContent());
            commentHolder.ratingBar.setRating(comment.getStar());
            commentHolder.name_tv.setText(comment.getNickName());
            Glide.with(context).load(comment.getAvatar())
                    .apply(new RequestOptions().placeholder(R.mipmap.test_head))
                    .into(commentHolder.head_iv);
        } catch (Exception e) {
        }
    }

    @Override
    public int getItemCount() {
        if (comments == null) return 0;
        return comments.size();
    }


    class CommentHolder extends RecyclerView.ViewHolder {

        RatingBar ratingBar;
        ReadMoreTextView commentText_rtv;
        TextView time_tv;
        TextView name_tv;
        CircleImageView head_iv;

        public CommentHolder(@NonNull View itemView) {
            super(itemView);
            ratingBar = itemView.findViewById(R.id.ratingBar);
            commentText_rtv = itemView.findViewById(R.id.commentText_rtv);
            commentText_rtv.setTrimCollapsedText("展开");
            commentText_rtv.setTrimExpandedText("收起");
            time_tv = itemView.findViewById(R.id.time_tv);
            name_tv = itemView.findViewById(R.id.name_tv);
            head_iv = itemView.findViewById(R.id.head_iv);
        }
    }
}
